#include "Keys.h"

// 定义为静态变量，仅在本文件可见
static u8 state_key1 = UP;
static u8 state_key2 = UP;
static u8 state_key3 = UP;
static u8 state_key4 = UP;

//准双向口
static void GPIO_config(void) {
    //准双向口	P51-P54
    P5_MODE_IO_PU(GPIO_Pin_1);
    P5_MODE_IO_PU(GPIO_Pin_2);
    P5_MODE_IO_PU(GPIO_Pin_3);
    P5_MODE_IO_PU(GPIO_Pin_4);
}

// 初始化
void Key_Init(){
	GPIO_config();
}

// 按键扫描，扫描KEY1~KEY4按下、弹起
void Key_Scan(void){
    //KEY1按下
    if( KEY1 == LOW && state_key1 == UP ){
        if(USE_KEY_DOWN == 1){
            state_key1 = DOWN;
          	
        }
    //KEY1弹起	
    }else if( KEY1 == HIGH && state_key1 == DOWN ){
        if(USE_KEY_ON == 1){
            state_key1 = UP;
            	
        }
    }
    //KEY2按下
    if( KEY2 == LOW && state_key2 == UP ){
        if(USE_KEY_DOWN == 1){
            state_key2 = DOWN;

        } 	
    }
    //KEY2弹起
    else if( KEY2 == HIGH && state_key2 == DOWN ){
        if(USE_KEY_ON == 1){
            state_key2 = UP;

        }

    }
    //KEY3按下
    if( KEY3 == LOW && state_key3 == UP ){
        if(USE_KEY_DOWN == 1){
            state_key3 = DOWN;

        }
    }
    //KEY3弹起
    else if( KEY3 == HIGH && state_key3 == DOWN ){
        if(USE_KEY_ON == 1){
            state_key3 = UP;

        }

    }
    //KEY4按下
    if( KEY4 == LOW && state_key4 == UP ){
        if(USE_KEY_DOWN == 1){
            state_key4 = DOWN;

        }

    }
    //KEY4弹起
    else if( KEY4 == HIGH && state_key4 == DOWN ){
        if(USE_KEY_ON == 1){
            state_key4 = UP;
	
        } 	
    }

}